# 引入time模块，实现延时
import time
# 引入By Class,辅助元素定位
from selenium.webdriver.common.by import By
class Answer:
    #提交
    def submit(self, driver, index):
        classes = driver.find_elements(By.CSS_SELECTOR, '[class=" book_unit_btn"]')
        classes[index-1].click()
        time.sleep(0.5)
        driver.find_element(By.CSS_SELECTOR, '[class="layui-layer-btn0"]').click()
        time.sleep(0.5)
        driver.find_element(By.CSS_SELECTOR, '[class="layui-layer-btn0"]').click()
        time.sleep(0.5)
    #选择题
    def choice_question(self, driver, answers, index, counts, time1):
        start = 1
        end = len(answers)+1
        for i in range(start, end):
            num = str(i)
            values = driver.find_elements(By.ID, num)
            count = counts[i-1]
            if answers[i-1] == "A":
                for j in range((count-1)*4, count*4):
                    if 'A' in values[j].get_attribute('value'):
                        time.sleep(time1)
                        values[j].click()
                        break
            elif answers[i-1] == "B":
                for j in range((count-1)*4, count*4):
                    if 'B' in values[j].get_attribute('value'):
                        time.sleep(time1)
                        values[j].click()
                        break
            elif answers[i-1] == "C":
                for j in range((count-1)*4, count*4):
                    if 'C' in values[j].get_attribute('value'):
                        time.sleep(time1)
                        values[j].click()
                        break
            elif answers[i-1] == "D":
                for j in range((count-1)*4, count*4):
                    if 'D' in values[j].get_attribute('value'):
                        time.sleep(time1)
                        values[j].click()
                        break
        Answer.submit(self, driver, index)
    # 填空题
    def vacancy_question(self, driver, answers, index, count):
        for i in range(count):
            xpath = '//*[@id="' + str(i+1) + '"]'
            driver.find_element(By.XPATH, xpath).send_keys(answers[i])
        Answer.submit(self, driver, index)